# 
MK_DIR = $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
# 规划版本。用于区分功能和BUG阶段。
BASE_VERSION ?= 1.0
@echo "规划版本: $(BASE_VERSION)"
# 代码版本。用于细分安装包所在代码版本。
GIT_VERSION ?= $(shell git rev-parse --short HEAD)
@echo "代码版本: $(GIT_VERSION)"
# 动态版本。用于重复构建区分镜像版本。
META_VERSION ?= $(shell expr `date +"%y%m%d"` + 0).$(shell expr `date +"%H%M"` + 0)
@echo "动态版本: $(META_VERSION)"

# 
PKGNAME ?= $(shell cat go.mod |head -n1 |awk '{print $2}')
IMAGE_TAG ?= $(META_VERSION).$(GIT_VERSION)

# 修改代码中的版本信息值
METADATA_VAR = Version=$(META_VERSION)
METADATA_VAR += CommitSHA=$(GIT_VERSION)
GO_LDFLAGS = $(METADATA_VAR:%=-X $(PKGNAME)/global.%)
GO_TAGS ?=

BUILD_DIR ?= $(MK_DIR)/build
BUILD_IMAGE_DIR = $(MK_DIR)/build/image
BUILD_IMAGE_ENGINE ?= docker

PUBLIC_DIR ?= $(BUILD_DIR)/public
DEPLOY_DIR ?= $(BUILD_DIR)/deploy

RELEASE_IMAGE_PREFIX=swr.cn-north-4.myhuaweicloud.com/synctech/

SEPARATOR_LINE="-----------------------------------------------------"

#########################################################################################################################################

RELEASE_EXES_API = api
RELEASE_EXES = $(RELEASE_EXES_API)

#########################################################################################################################################
.DEFAULT_GOAL := all
.PHONY: clean
clean: clean-bin clean-image
clean-bin: $(RELEASE_EXES:%=%-clean)
clean-image: $(RELEASE_EXES:%=%-image-clean)
.PHONY: all
all: bin
bin: $(RELEASE_EXES:%=%)
image: $(RELEASE_EXES:%=%-image)
	@$(BUILD_IMAGE_ENGINE) image prune -f
image-push: $(RELEASE_EXES:%=%-image-push)
image-push-clean: $(RELEASE_EXES:%=%-image-push) clean
	@$(BUILD_IMAGE_ENGINE) image prune -f
#########################################################################################################################################
setenv:
	git config --global url."git@gitee.com:".insteadOf "https://gitee.com/"
	$(eval CGO_ENABLED=0)
	$(eval GOARCH=amd64)
	$(eval GOOS=linux)
	$(eval GO111MODULE=auto)
	$(eval GOPROXY=https://repo.huaweicloud.com/repository/goproxy/,https://goproxy.cn,https://mirrors.aliyun.com/goproxy,direct)
	$(eval GOPRIVATE=192.168.1.64,*gitee.com,*.corp.example.com,rsc.io/private)
# # vfsgen
# ifeq ($(shell which vfsgen |wc -l),0)
# 	@echo "未发现vfsgen，准备安装"
# 	go install gitee.com/chenhonghua/ginorigin/utils/vfsgen@v0.1.5
# endif
	go-bindata -o files/files.go -pkg files files
	@echo "内置文件编译完成: $(MK_DIR)/files/files.go"
#########################################################################################################################################
# 清理bin文件
$(RELEASE_EXES:%=%-clean):
	@echo "$(SEPARATOR_LINE) OPTION: $(@F) $(SEPARATOR_LINE)"
	@-rm -rf $(BUILD_IMAGE_DIR)/$(@F:%-clean=%)
# 编译
$(RELEASE_EXES): %: %-clean setenv
	@echo "$(SEPARATOR_LINE) OPTION: $(@F) $(SEPARATOR_LINE)"
	@go clean
	@# -go get
	@go mod vendor
	@vfsgen -s page/$(@F) -p page -v Dir -o $(MK_DIR)/page/page.go
	@CGO_ENABLED=0 GOBIN=$(BUILD_IMAGE_DIR) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" -buildvcs=false -mod=vendor $(PKGNAME)/cmd/$(@F)
	@echo "编译完成: $(BUILD_IMAGE_DIR)/$(@F)"
##########################################################################################################################################

#########################################################################################################################################
# 清理镜像
$(RELEASE_EXES:%=%-image-clean):
	@echo "$(SEPARATOR_LINE) OPTION: $(@F) $(SEPARATOR_LINE)"
	@-$(BUILD_IMAGE_ENGINE) images |grep $(RELEASE_IMAGE_PREFIX)$(@F:%-image-clean=%) |awk '{print $$1":"$$2}' |xargs $(BUILD_IMAGE_ENGINE) rmi
	@-$(BUILD_IMAGE_ENGINE) image prune -f
# 构建镜像
$(RELEASE_EXES:%=%-image): %-image: %
	@echo "$(SEPARATOR_LINE) OPTION: $(@F) $(SEPARATOR_LINE)"
	@$(eval CURDEST=$(@F:%-image=%))
	@$(BUILD_IMAGE_ENGINE) build --no-cache --build-arg APP=$(CURDEST) -t $(RELEASE_IMAGE_PREFIX)$(CURDEST) $(BUILD_IMAGE_DIR)/
	@#echo "镜像构建完成: $(RELEASE_IMAGE_PREFIX)$(CURDEST)"
	@$(BUILD_IMAGE_ENGINE) tag $(RELEASE_IMAGE_PREFIX)$(CURDEST) $(RELEASE_IMAGE_PREFIX)$(CURDEST):$(IMAGE_TAG)
	@echo "镜像构建完成: $(RELEASE_IMAGE_PREFIX)$(CURDEST):$(IMAGE_TAG)"
# 构建并推送镜像
$(RELEASE_EXES:%=%-image-push): %-image-push: %-image
	@echo "$(SEPARATOR_LINE) OPTION: $(@F) $(SEPARATOR_LINE)"
	@$(eval CURDEST=$(@F:%-image-push=%))
	@$(BUILD_IMAGE_ENGINE) push $(RELEASE_IMAGE_PREFIX)$(CURDEST)
	@#echo "镜像推送完成: $(RELEASE_IMAGE_PREFIX)$(CURDEST)"
	@$(BUILD_IMAGE_ENGINE) push $(RELEASE_IMAGE_PREFIX)$(CURDEST):$(IMAGE_TAG)
	@echo "镜像推送完成: $(RELEASE_IMAGE_PREFIX)$(CURDEST):$(IMAGE_TAG)"
##########################################################################################################################################


